﻿package
{
	import ben.asset.Loading;
	import ben.SomeTools;
	import com.greensock.TweenMax;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	/**
	* ...
	* @author Ben
	*/
	public class Preload extends Sprite
	{
		public var _logo:Sprite
		public var _load:Loading
		public var _content:Sprite
		public var _debug:TextField
		public var _trame:Sprite
		public var _bg:Sprite
		
		private var _i:Infos
		private var _cl:Loader;
		private var _xl:URLLoader;
		private var _bgt:Shape
		
		public function Preload() 
		{
			// GLOBAL VARS
			_i = Infos.getInstance();
			_i.sceneWidth = 640;
			
			_debug.autoSize = TextFieldAutoSize.LEFT;
			_debug.mouseEnabled = false;
			
			_i.theDB = _debug;
			
			// CLIC DROIT
			var _cm:ContextMenu = new ContextMenu();
			_cm.hideBuiltInItems();
			var copyright:ContextMenuItem = new ContextMenuItem("Copyright © bigbenprod.fr 2011.");
			_cm.customItems.push(copyright);
			
			contextMenu = _cm;
			
			// STAGE
			stage.align = StageAlign.TOP;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.addEventListener(Event.RESIZE, resizeHandler);
						
			stage.doubleClickEnabled = true;
			stage.addEventListener(MouseEvent.DOUBLE_CLICK, goFull);
			
			// FLASHVARS
			_i.theFV = root.loaderInfo.parameters;
			
			// TRAME
			_bgt = new Shape();
			SomeTools.getVeilSh(stage.stageWidth, stage.stageHeight, _trame, _bgt);
			_trame.mouseEnabled = false;
			
			// RESIZE
			resizeHandler();
			
			// LET'S GO
			loadData();
		}
		
		private function loadData():void
		{
			_xl = new URLLoader();
			_xl.addEventListener(Event.COMPLETE, loadContent);
			_xl.addEventListener(IOErrorEvent.IO_ERROR, errorData);
			_xl.load(new URLRequest("xml/lmx.xml"));
		}
		
		private function errorData(e:IOErrorEvent):void 
		{
			_i.theDB.text = "[ERROR XML] " + e.text;
		}
		
		private function loadContent(e:Event = null):void 
		{	
			_i.theXML = XML((e.target as URLLoader).data);
			
			_cl = new Loader();
			_cl.contentLoaderInfo.addEventListener(Event.COMPLETE, contentReady);
			_cl.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorContent);
			_cl.load(new URLRequest("main.swf"));
		}
		
		private function errorContent(e:IOErrorEvent):void 
		{
			_i.theDB.text = "[ERROR CONTENT] " + e.text;
		}
				
		private function contentReady(e:Event):void 
		{
			_cl.contentLoaderInfo.removeEventListener(Event.COMPLETE, contentReady);
			_content.addChild(_cl);
			resizeHandler();
			TweenMax.to(_load, 1, { alpha:0 } );
			TweenMax.to(_logo, 1, { alpha:0, delay:1, onComplete:onOpen } );
		}
		
		private function onOpen():void
		{
			_load.visible = false;
			_logo.visible = false;
			TweenMax.to(_bg, 1, { y:stage.stageHeight-5, onComplete:hideBg } );
			(_cl.content as Main).open();
		}
		
		private function hideBg():void
		{
			_bg.visible = false;
		}
		
		private function goFull(e:MouseEvent):void 
		{
			if (stage.displayState == StageDisplayState.FULL_SCREEN)
			{
				stage.displayState = StageDisplayState.NORMAL;
			}
			else 
			{
				stage.displayState = StageDisplayState.FULL_SCREEN;
			}
		}
		
		private function resizeHandler(e:Event = null):void 
		{	
			_bg.width = stage.stageWidth;
			_bg.height = stage.stageHeight;
			_bg.x = _trame.x = -Math.round((stage.stageWidth - _i.sceneWidth) / 2);
			
			_load.y = Math.round(stage.stageHeight / 2) + 100;
			_logo.y = Math.round(stage.stageHeight / 2);
			
			// TRAME
			SomeTools.getVeilSh(stage.stageWidth, stage.stageHeight, _trame, _bgt);
			
		}
		
	}
	
}
